//
//  ShowNetImagesVC.m
//  XLImageViewerExample
//
//  Created by lixiaoyi on 2018/7/24.
//  Copyright © 2018年 MengXianLiang. All rights reserved.
//

#import "YZShowNetImagesVC.h"
#import "YZImageCell.h"

@interface YZShowNetImagesVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UIActionSheetDelegate>

@property (nonatomic, strong) NSArray *imageUrls;

@property (nonatomic, assign) NSInteger selectIndex;

@property (nonatomic,strong) UICollectionView *collectionView;

@end

@implementation YZShowNetImagesVC

- (instancetype)initWithImageUrls:(NSArray *)imageUrls andSelectIndex:(NSInteger)selectindex
{
    if (self = [super init]) {
        _imageUrls = imageUrls;
        _selectIndex = selectindex;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    if (self.selectIndex == 0) return;
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.selectIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
}


#pragma mark --  CollectionViewDelegate&DataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageUrls.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellId = @"ImageCell";
    YZImageCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    cell.imageUrl = self.imageUrls[indexPath.row];
    return  cell;
}

#pragma mark -- setters && getters

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.itemSize = CGSizeMake(kScreenWidth,self.view.bounds.size.height);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        [_collectionView registerClass:[YZImageCell class] forCellWithReuseIdentifier:@"ImageCell"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = YES;
        _collectionView.backgroundColor = [UIColor clearColor];
    }
    return _collectionView;
}

@end
